;
define(function(require, exports, module) {
	'use strict';
	var base = require("base");
	var dialog = require("dialog");
	var html_valuationCal = require("./valuationCal.html");
	var css_valuationCal = require("./valuationCal.css");
	var databind = require("databind");
	var pageIndex = 1,
		pageSize = 5,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化

	var E = {};

	E.price = 0;
/**
 * 
 * @param {Object} title//弹出框标题
 * @param {Object} bulkstockInfoId 货物ID
 * @param {Object} unit 单位 
 * @param {Object} quantity 货物数量
 * @param {Object} dom_Valuation 货物总估值
 * @param {Object} dom_Price 货物单价估值
 * @param {Object} conversionFactor  单价换算系数
 */
	E.showCal = function(title, bulkstockInfoId,unit, quantity, dom_Valuation,dom_Price,conversionFactor, callback) {
		pageIndex = 1;
		$("#popdialog").html(html_valuationCal);
		dialog.showDialog($("#valuationCalDialog"));
		$("#valuationCalDialog .dialogTitle").html(title);
		$("#valuationCalDialog .valuationInputUnit").html(unit);
		$("#valuationCalDialog .valuationQuantity").val(quantity);
		databind.fillInput($("#valuationCalDialog"), {
			action: "inquireBulkstockGatherLatestList.json?bulkstockInfoId=" + bulkstockInfoId
		});
		inquireBulkstockLatestCollectionList(bulkstockInfoId,conversionFactor);

		$("#valuationCalCancel").click(function() {
			dialog.hideDialog($("#valuationCalDialog"));
		});

		$("#valuationCalSure").click(function() {
			var valuationRegionPrice = $("#valuationCalContentDetail .valuationRegionPrice").val();
			var valuationQuantity = $("#valuationCalContentDetail .valuationQuantity").val();
			E.price = (valuationRegionPrice * valuationQuantity).toFixed(2);
			$(dom_Valuation).val(E.price);
			$(dom_Valuation).html(E.price);
			$(dom_Price).val(valuationRegionPrice);
			$(dom_Price).html(valuationRegionPrice);
			dialog.hideDialog($("#valuationCalDialog"));
			callback();
		});
		

		$("#valuationCalContentDetail .valuationRegionPrice").on("change", function() {
			var valuationRegionPrice = $("#valuationCalContentDetail .valuationRegionPrice").val();
			var valuationQuantity = $("#valuationCalContentDetail .valuationQuantity").val();
			if (valuationRegionPrice == "") {
				valuationRegionPrice = 0;
			}
			if (valuationQuantity == "") {
				valuationQuantity = 0;
			}
			$("#valuationCalPrice").html((valuationRegionPrice * valuationQuantity).toFixed(2));
		});
		
		$("#valuationCalContentDetail .valuationQuantity").on("change", function() {
			var valuationRegionPrice = $("#valuationCalContentDetail .valuationRegionPrice").val();
			var valuationQuantity = $("#valuationCalContentDetail .valuationQuantity").val();
			if (valuationRegionPrice == "") {
				valuationRegionPrice = 0;
			}
			if (valuationQuantity == "") {
				valuationQuantity = 0;
			}
			$("#valuationCalPrice").html((valuationRegionPrice * valuationQuantity).toFixed(2));
		});
	};

	//最新商品采集价格列表
	var inquireBulkstockLatestCollectionList = function(bulkstockInfoId,conversionFactor) {
		base.fnAjax({
			action: "inquireBulkstockLatestCollectionList.json",
			async: false,
			oData: {
				pageSize: pageSize,
				pageIndex: pageIndex,
				bulkstockInfoId: bulkstockInfoId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#valuationCalDataListTpl").html();
					var html = juicer(tpl, data);
					$("#valuationCalDataListBody").html(html);

					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl($("#valuationCalPagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireBulkstockLatestCollectionList(bulkstockInfoId,conversionFactor);
						});
					
					riseAndFallColor();
					
					$("#valuationCalContentDetail .priceCheck").on("click", function() {
						var price = parseFloat($(this).parents("tr").find(".price").html())*conversionFactor;
						$("#valuationCalContentDetail .valuationRegionPrice").val(price.toFixed(5));
						var valuationRegionPrice = $("#valuationCalContentDetail .valuationRegionPrice").val();
						var valuationQuantity = $("#valuationCalContentDetail .valuationQuantity").val();
						if (valuationRegionPrice == "") {
							valuationRegionPrice = 0;
						}
						if (valuationQuantity == "") {
							valuationQuantity = 0;
						}
						$("#valuationCalPrice").html((valuationRegionPrice * valuationQuantity).toFixed(2));
					});
					
					$(".publicMonitor").unbind("click").click(function(){
						var bulkstockname = $(this).parents("tr").attr("bulkstockname");
						var specification = $(this).parents("tr").attr("specification");
						var latestgatherId = $(this).parents("tr").attr("latestgatherid");
						var region = $(this).parents("tr").attr("region");
//						location.href="../publicInquire/publicInquireCollectionMonitor.html?bulkstockName="+encodeURI(encodeURI(bulkstockname))+"&specification="+encodeURI(encodeURI(specification))+"&region="+encodeURI(encodeURI(region))+"&latestgatherId="+latestgatherId;
						window.open("../publicInquire/publicInquireCollectionMonitor.html?bulkstockName="+encodeURI(encodeURI(bulkstockname))+"&specification="+encodeURI(encodeURI(specification))+"&region="+encodeURI(encodeURI(region))+"&latestgatherId="+latestgatherId);
					});
				} else {
					//                  base.alert(2,"信息获取失败");
				}
			}
		});
	};
	
	//改变涨跌颜色
	var riseAndFallColor = function() {
		var riseAndFall = $(".riseAndFall");
		for (var i = 0, j = riseAndFall.length; i < j; i++) {
			if (riseAndFall[i].innerHTML.charAt(0) == "-") {
				$(riseAndFall[i]).css("color", "#33A55E");
			} else if (riseAndFall[i].innerHTML.charAt(0) == "+") {
				$(riseAndFall[i]).css("color", "#F1592A");
			} else {
			}
		}
	};

	E.init = function() {
		alert(123);
	}

	return E;
});